package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.R;
import com.atguigu.feign.clients.ProductService;
import com.atguigu.feign.to.SkuHasStockTo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.service.WareSkuService;

@Slf4j
@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    private ProductService productService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                new QueryWrapper<WareSkuEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        /*{wareId: 123,//仓库id
           skuId: 123//商品id}*/
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        String wareId = (String) params.get("wareId");
        if(StringUtils.isNotBlank(wareId)){
            queryWrapper.eq(WareSkuEntity::getWareId,wareId);
        }

        String skuId = (String) params.get("skuId");
        if(StringUtils.isNotBlank(skuId)){
            queryWrapper.eq(WareSkuEntity::getSkuId,skuId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    //成功采购的采购项存入库中。
    @Override
    public void addSkuStock(Long skuId, Integer skuNum, Long wareId) {
        LambdaQueryWrapper<WareSkuEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WareSkuEntity::getSkuId,skuId).eq(WareSkuEntity::getWareId,wareId);
        WareSkuEntity one = this.getOne(queryWrapper);

        //先查库判断当前采购项 是否已经存在采购
        if(one == null){
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId).setStock(skuNum).setWareId(wareId).setStockLocked(0);
            //新建WareSku 冗余保存sku名字。 调用远程接口。
            //如果远程获取失败整个事务不需要全部回滚。 TODO 还可以用其他办法让异常不会滚 高级篇.
            try {
                R r = productService.skuInfo(skuId);

                if(r.getCode() == 0){
                    Map<String,Object> spuInfo = (Map<String, Object>) r.get("skuInfo");
                    wareSkuEntity.setSkuName((String) spuInfo.get("skuName"));
                }
            } catch (Exception e) {
            }

            log.info("新建库存：{}",skuId);
            wareSkuDao.insert(wareSkuEntity);
        }else{
            log.info("添加库存：{}",skuId);
            wareSkuDao.addSkuStock(skuId,skuNum,wareId);
        }
    }

    //根据skuID 查出是否还有库存
    @Override
    public List<SkuHasStockTo> hasstock(List<Long> skuIds) {
        List<SkuHasStockTo> collect = skuIds.stream().map(skuId -> {
            //根据skuid 查询返回库存数量
            Long num = this.baseMapper.hasstockBySkuId(skuId);
            SkuHasStockTo skuHasStockTo = new SkuHasStockTo();
            skuHasStockTo.setSkuId(skuId);
            if(num != null && num > 0){
                skuHasStockTo.setHasStock(true);
            }else{
                skuHasStockTo.setHasStock(false);
            }
            return skuHasStockTo;
        }).collect(Collectors.toList());

        return collect;
    }

}